﻿//2013.5.13  guxiaoli
ManualInvType = "N_VAT",//暂存当前票据类型
MSplitGroup = 0;//标记手工拆分当前最大组数
ManualDetailPrice = 0;//暂存被拆分明细总单价
ManualDetailQuantity = 0;//暂存被拆分明细总数量
ManualDetailNTA = 0;//暂存被拆分明细总不含税金额
ManualDetailTA = 0;//暂存被拆分明细总含税金额
ManualX = 1;//计价方式为0:1，计价方式为1:1+rate
ManualR = 0;//暂存被拆分明细税率
ManualDR = 0;//暂存被拆分明细折扣率

function TypeOneCount(comp,newValue,oldValue,eOpts){//单价(数量不变),修改单价
if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,6})?$/.test(newValue)){
	var P2 = parseFloat(newValue);
	var Q2= Q1 = parseFloat(Ext.getCmp('detail1-quantity').getValue());
	var A2 = Share.RetainDecimal(P2*Q2);
	var T2 = Share.RetainDecimal(P2*Q2*ManualR/ManualX);
	var DA2 = Share.RetainDecimal(P2*Q2*ManualDR);
	var DT2 = Share.RetainDecimal(P2*Q2*ManualDR*ManualR/ManualX);   
	var NTA2 = Share.RetainDecimal(P2*Q2/ManualX);
	var TA2 = Share.RetainDecimal(P2*Q2*(1+ManualR)/ManualX);
	Ext.getCmp('detail2-NTA').setValue(NTA2);
	Ext.getCmp('detail2-TA').setValue(TA2);
	Ext.getCmp('detail2-amount').setValue(A2);
	Ext.getCmp('detail2-tax').setValue(T2);
	Ext.getCmp('detail2-disamount').setValue(DA2);
	Ext.getCmp('detail2-distax').setValue(DT2);
	
	var P1 = Share.RetainDecimal(ManualDetailPrice-P2,6);
	var A1 = Share.RetainDecimal(P1*Q1);
	var T1 = Share.RetainDecimal(P1*Q1*ManualR/ManualX);
	var DA1 = Share.RetainDecimal(P1*Q1*ManualDR);
	var DT1 = Share.RetainDecimal(P1*Q1*ManualDR*ManualR/ManualX);   
	var NTA1 = Share.RetainDecimal(P1*Q1/ManualX);
	var TA1 = Share.RetainDecimal(P1*Q1*(1+ManualR)/ManualX);
	Ext.getCmp('detail1-price').setValue(P1);
	Ext.getCmp('detail1-NTA').setValue(NTA1);
	Ext.getCmp('detail1-TA').setValue(TA1);
	Ext.getCmp('detail1-amount').setValue(A1);
	Ext.getCmp('detail1-tax').setValue(T1);
	Ext.getCmp('detail1-disamount').setValue(DA1);
	Ext.getCmp('detail1-distax').setValue(DT1);
	
	var oriA = parseFloat(Ext.getCmp('detail-amount').getValue());
	var oriT = parseFloat(Ext.getCmp('detail-tax').getValue());
	var oriDA = parseFloat(Ext.getCmp('detail-disamount').getValue());
	var oriDT = parseFloat(Ext.getCmp('detail-distax').getValue());
	Ext.getCmp('detail-differ-amount').setValue(Share.RetainDecimal(oriA-parseFloat(NTA1)-parseFloat(NTA2)));
	Ext.getCmp('detail-differ-tax').setValue(Share.RetainDecimal(oriT-parseFloat(T1)-parseFloat(T2)));
	Ext.getCmp('detail-differ-disamount').setValue(Share.RetainDecimal(oriDA-parseFloat(DA1)-parseFloat(DA2)));
	Ext.getCmp('detail-differ-distax').setValue(Share.RetainDecimal(oriDT-parseFloat(DT1)-parseFloat(DT2)));
}       		
};
function TypeTwoCount(comp,newValue,oldValue,eOpts){//不含税金额（单价不变），修改金额
	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(newValue)){
		var P2 = P1 = parseFloat(Ext.getCmp('detail1-price').getValue());
		var NTA2 = parseFloat(newValue);
		var TA2 = Share.RetainDecimal(NTA2*(1+ManualR));
		var A2 = Share.RetainDecimal(NTA2*ManualX);
		var T2 = Share.RetainDecimal(NTA2*ManualR);
		var DA2 = Share.RetainDecimal(NTA2*ManualX*ManualDR);
		var DT2 = Share.RetainDecimal(NTA2*ManualDR*ManualR);       	
		Ext.getCmp('detail2-TA').setValue(TA2);
		Ext.getCmp('detail2-quantity').setValue(Share.RetainDecimal(NTA2*ManualX/P2,6));
		Ext.getCmp('detail2-amount').setValue(A2);
		Ext.getCmp('detail2-tax').setValue(T2);
		Ext.getCmp('detail2-disamount').setValue(DA2);
		Ext.getCmp('detail2-distax').setValue(DT2);
		
		var NTA1 = Share.RetainDecimal(ManualDetailNTA-NTA2);
		var TA1 = Share.RetainDecimal(NTA1*(1+ManualR));
		var A1 = Share.RetainDecimal(NTA1*ManualX);
		var T1 = Share.RetainDecimal(NTA1*ManualR);
		var DA1 = Share.RetainDecimal(NTA1*ManualX*ManualDR);
		var DT1 = Share.RetainDecimal(NTA1*ManualDR*ManualR);      
		Ext.getCmp('detail1-NTA').setValue(NTA1);
		Ext.getCmp('detail1-TA').setValue(TA1);
		Ext.getCmp('detail1-quantity').setValue(Share.RetainDecimal(NTA1*ManualX/P1,6));
		Ext.getCmp('detail1-amount').setValue(A1);
		Ext.getCmp('detail1-tax').setValue(T1);
		Ext.getCmp('detail1-disamount').setValue(DA1);
		Ext.getCmp('detail1-distax').setValue(DT1);
		
		var oriA = parseFloat(Ext.getCmp('detail-amount').getValue());
		var oriT = parseFloat(Ext.getCmp('detail-tax').getValue());
		var oriDA = parseFloat(Ext.getCmp('detail-disamount').getValue());
		var oriDT = parseFloat(Ext.getCmp('detail-distax').getValue());
		Ext.getCmp('detail-differ-amount').setValue(Share.RetainDecimal(oriA-parseFloat(NTA1)-parseFloat(NTA2)));
		Ext.getCmp('detail-differ-tax').setValue(Share.RetainDecimal(oriT-parseFloat(T1)-parseFloat(T2)));
		Ext.getCmp('detail-differ-disamount').setValue(Share.RetainDecimal(oriDA-parseFloat(DA1)-parseFloat(DA2)));
		Ext.getCmp('detail-differ-distax').setValue(Share.RetainDecimal(oriDT-parseFloat(DT1)-parseFloat(DT2)));
	}       		
};
function TypeThreeCount(comp,newValue,oldValue,eOpts){//含税金额（单价不变），修改税额
	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(newValue)){
 	var P2 = P1 = parseFloat(Ext.getCmp('detail1-price').getValue());
 	var TA2 = parseFloat(newValue);
 	var NTA2 = Share.RetainDecimal(TA2/(1+ManualR)); 	
 	var T2 = Share.RetainDecimal(TA2*ManualR/(1+ManualR));
 	var A2 = Share.RetainDecimal(TA2*ManualX/(1+ManualR));
 	var Q2 = Share.RetainDecimal((TA2*ManualX/(1+ManualR))/P2,6);
 	var DA2 = Share.RetainDecimal((TA2*ManualX/(1+ManualR))*ManualDR);
 	var DT2 = Share.RetainDecimal((TA2*ManualR/(1+ManualR))*ManualDR);  
 	Ext.getCmp('detail2-NTA').setValue(NTA2);
	Ext.getCmp('detail2-amount').setValue(A2);
	Ext.getCmp('detail2-tax').setValue(T2);
	Ext.getCmp('detail2-quantity').setValue(Q2);
	Ext.getCmp('detail2-disamount').setValue(DA2);
	Ext.getCmp('detail2-distax').setValue(DT2);
			
	var TA1 = Share.RetainDecimal(ManualDetailTA-TA2);
	var NTA1 = Share.RetainDecimal(TA1/(1+ManualR));
	var T1 = Share.RetainDecimal(TA1*ManualR/(1+ManualR));
 	var A1 = Share.RetainDecimal(TA1*ManualX/(1+ManualR));
 	var Q1 = Share.RetainDecimal((TA1*ManualX/(1+ManualR))/P1,6);
 	var DA1 = Share.RetainDecimal((TA1*ManualX/(1+ManualR))*ManualDR);
 	var DT1 = Share.RetainDecimal((TA1*ManualR/(1+ManualR))*ManualDR);    
 	Ext.getCmp('detail1-TA').setValue(TA1);
 	Ext.getCmp('detail1-NTA').setValue(NTA1);
	Ext.getCmp('detail1-quantity').setValue(Q1);
	Ext.getCmp('detail1-amount').setValue(A1);
	Ext.getCmp('detail1-tax').setValue(T1);
	Ext.getCmp('detail1-disamount').setValue(DA1);
	Ext.getCmp('detail1-distax').setValue(DT1);
			
	var oriA = parseFloat(Ext.getCmp('detail-amount').getValue());
	var oriT = parseFloat(Ext.getCmp('detail-tax').getValue());
	var oriDA = parseFloat(Ext.getCmp('detail-disamount').getValue());
	var oriDT = parseFloat(Ext.getCmp('detail-distax').getValue());
	Ext.getCmp('detail-differ-amount').setValue(Share.RetainDecimal(oriA-parseFloat(NTA1)-parseFloat(NTA2)));
	Ext.getCmp('detail-differ-tax').setValue(Share.RetainDecimal(oriT-parseFloat(T1)-parseFloat(T2)));
	Ext.getCmp('detail-differ-disamount').setValue(Share.RetainDecimal(oriDA-parseFloat(DA1)-parseFloat(DA2)));
	Ext.getCmp('detail-differ-distax').setValue(Share.RetainDecimal(oriDT-parseFloat(DT1)-parseFloat(DT2)));
	}       		
};
function TypeFourCount(comp,newValue,oldValue,eOpts){//数量（单价不变），修改数量
	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,6})?$/.test(newValue)){
	var P2 =P1= parseFloat(Ext.getCmp('detail1-price').getValue());
	var Q2 = parseFloat(newValue);
	var A2 = Share.RetainDecimal(P2*Q2);
	var T2 = Share.RetainDecimal(P2*Q2*ManualR/ManualX);
	var DA2 = Share.RetainDecimal(P2*Q2*ManualDR);
	var DT2 = Share.RetainDecimal(P2*Q2*ManualDR*ManualR/ManualX);  
	var NTA2 = Share.RetainDecimal(P2*Q2/ManualX);
	var TA2 = Share.RetainDecimal(P2*Q2*(1+ManualR)/ManualX);
	Ext.getCmp('detail2-NTA').setValue(NTA2);
	Ext.getCmp('detail2-TA').setValue(TA2);
	Ext.getCmp('detail2-amount').setValue(A2);
	Ext.getCmp('detail2-tax').setValue(T2);
	Ext.getCmp('detail2-disamount').setValue(DA2);
	Ext.getCmp('detail2-distax').setValue(DT2);
				
	var Q1 = Share.RetainDecimal(ManualDetailQuantity-Q2,6);
	var A1 = Share.RetainDecimal(P1*Q1);
	var T1 = Share.RetainDecimal(P1*Q1*ManualR/ManualX);
	var DA1 = Share.RetainDecimal(P1*Q1*ManualDR);
	var DT1 = Share.RetainDecimal(P1*Q1*ManualDR*ManualR/ManualX);  
	var NTA1 = Share.RetainDecimal(P1*Q1/ManualX);
	var TA1 = Share.RetainDecimal(P1*Q1*(1+ManualR)/ManualX);
	Ext.getCmp('detail1-NTA').setValue(NTA1);
	Ext.getCmp('detail1-TA').setValue(TA1);
	Ext.getCmp('detail1-quantity').setValue(Q1);
	Ext.getCmp('detail1-amount').setValue(A1);
	Ext.getCmp('detail1-tax').setValue(T1);
	Ext.getCmp('detail1-disamount').setValue(DA1);
	Ext.getCmp('detail1-distax').setValue(DT1);
				
	var oriA = parseFloat(Ext.getCmp('detail-amount').getValue());
	var oriT = parseFloat(Ext.getCmp('detail-tax').getValue());
	var oriDA = parseFloat(Ext.getCmp('detail-disamount').getValue());
	var oriDT = parseFloat(Ext.getCmp('detail-distax').getValue());
	Ext.getCmp('detail-differ-amount').setValue(Share.RetainDecimal(oriA-parseFloat(NTA1)-parseFloat(NTA2)));
	Ext.getCmp('detail-differ-tax').setValue(Share.RetainDecimal(oriT-parseFloat(T1)-parseFloat(T2)));
	Ext.getCmp('detail-differ-disamount').setValue(Share.RetainDecimal(oriDA-parseFloat(DA1)-parseFloat(DA2)));
	Ext.getCmp('detail-differ-distax').setValue(Share.RetainDecimal(oriDT-parseFloat(DT1)-parseFloat(DT2)));
	}       		
};
function TypeFiveCount(comp,newValue,oldValue,eOpts){//不含税金额（数量不变），修改金额
	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(newValue)){	
		var Q2= Q1 = parseFloat(Ext.getCmp('detail1-quantity').getValue());
		var NTA2 = parseFloat(newValue);
		var TA2 = Share.RetainDecimal(NTA2*(1+ManualR));
		var A2 = Share.RetainDecimal(NTA2*ManualX);
		var P2 = Share.RetainDecimal(NTA2*ManualX/Q2,6);
		var T2 = Share.RetainDecimal(NTA2*ManualR);
		var DA2 = Share.RetainDecimal(NTA2*ManualX*ManualDR);
		var DT2 = Share.RetainDecimal(NTA2*ManualDR*ManualR);       		
		Ext.getCmp('detail2-price').setValue(P2);
		Ext.getCmp('detail2-TA').setValue(TA2);
		Ext.getCmp('detail2-amount').setValue(A2);
		Ext.getCmp('detail2-tax').setValue(T2);
		Ext.getCmp('detail2-disamount').setValue(DA2);
		Ext.getCmp('detail2-distax').setValue(DT2);
					
	var NTA1 = Share.RetainDecimal(ManualDetailNTA-NTA2);
	var TA1 = Share.RetainDecimal(NTA1*(1+ManualR));
	var A1 =  Share.RetainDecimal(NTA1*ManualX);
	var P1 = Share.RetainDecimal(NTA1*ManualX/Q1,6);
	var T1 = Share.RetainDecimal(NTA1*ManualR);
	var DA1 = Share.RetainDecimal(NTA1*ManualX*ManualDR);
	var DT1 = Share.RetainDecimal(NTA1*ManualDR*ManualR);      		
	Ext.getCmp('detail1-price').setValue(P1);
	Ext.getCmp('detail1-TA').setValue(TA1);
	Ext.getCmp('detail1-NTA').setValue(NTA1);
	Ext.getCmp('detail1-amount').setValue(A1);
	Ext.getCmp('detail1-tax').setValue(T1);
	Ext.getCmp('detail1-disamount').setValue(DA1);
	Ext.getCmp('detail1-distax').setValue(DT1);
					
	var oriA = parseFloat(Ext.getCmp('detail-amount').getValue());
	var oriT = parseFloat(Ext.getCmp('detail-tax').getValue());
	var oriDA = parseFloat(Ext.getCmp('detail-disamount').getValue());
	var oriDT = parseFloat(Ext.getCmp('detail-distax').getValue());
	Ext.getCmp('detail-differ-amount').setValue(Share.RetainDecimal(oriA-parseFloat(NTA1)-parseFloat(NTA2)));
	Ext.getCmp('detail-differ-tax').setValue(Share.RetainDecimal(oriT-parseFloat(T1)-parseFloat(T2)));
	Ext.getCmp('detail-differ-disamount').setValue(Share.RetainDecimal(oriDA-parseFloat(DA1)-parseFloat(DA2)));
	Ext.getCmp('detail-differ-distax').setValue(Share.RetainDecimal(oriDT-parseFloat(DT1)-parseFloat(DT2)));
   }       		
};
function TypeSixCount(comp,newValue,oldValue,eOpts){//含税金额（数量不变），修改单价
	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(newValue)){
	var Q2= Q1 = parseFloat(Ext.getCmp('detail1-quantity').getValue());
	var TA2 = parseFloat(newValue);
	var NTA2 = Share.RetainDecimal(TA2/(1+ManualR)); 
	var T2 = Share.RetainDecimal(TA2*ManualR/(1+ManualR));
	var A2 = Share.RetainDecimal(TA2*ManualX/(1+ManualR));
	var P2 = Share.RetainDecimal((TA2*ManualX/(1+ManualR))/Q2,6);
	var DA2 = Share.RetainDecimal((TA2*ManualX/(1+ManualR))*ManualDR);
	var DT2 = Share.RetainDecimal((TA2*ManualR/(1+ManualR))*ManualDR); 
	Ext.getCmp('detail2-NTA').setValue(NTA2);
	Ext.getCmp('detail2-price').setValue(P2); 		
	Ext.getCmp('detail2-amount').setValue(A2);
	Ext.getCmp('detail2-tax').setValue(T2);	
	Ext.getCmp('detail2-disamount').setValue(DA2);
	Ext.getCmp('detail2-distax').setValue(DT2);	
	
	var TA1 = Share.RetainDecimal(ManualDetailTA-TA2);
	var NTA1 = Share.RetainDecimal(TA1/(1+ManualR)); 
	var T1 = Share.RetainDecimal(TA1*ManualR/(1+ManualR));
	var A1 = Share.RetainDecimal(TA1*ManualX/(1+ManualR));
	var P1 = Share.RetainDecimal((TA1*ManualX/(1+ManualR))/Q1,6);
	var DA1 = Share.RetainDecimal((TA1*ManualX/(1+ManualR))*ManualDR);
	var DT1 = Share.RetainDecimal((TA1*ManualR/(1+ManualR))*ManualDR);   
	Ext.getCmp('detail1-NTA').setValue(NTA1);
	Ext.getCmp('detail1-TA').setValue(TA1);
	Ext.getCmp('detail1-price').setValue(P1);
	Ext.getCmp('detail1-amount').setValue(A1);
	Ext.getCmp('detail1-tax').setValue(T1);
	Ext.getCmp('detail1-disamount').setValue(DA1);
	Ext.getCmp('detail1-distax').setValue(DT1);
						
	var oriA = parseFloat(Ext.getCmp('detail-amount').getValue());
	var oriT = parseFloat(Ext.getCmp('detail-tax').getValue());
	var oriDA = parseFloat(Ext.getCmp('detail-disamount').getValue());
	var oriDT = parseFloat(Ext.getCmp('detail-distax').getValue());
	Ext.getCmp('detail-differ-amount').setValue(Share.RetainDecimal(oriA-parseFloat(NTA1)-parseFloat(NTA2)));
	Ext.getCmp('detail-differ-tax').setValue(Share.RetainDecimal(oriT-parseFloat(T1)-parseFloat(T2)));
	Ext.getCmp('detail-differ-disamount').setValue(Share.RetainDecimal(oriDA-parseFloat(DA1)-parseFloat(DA2)));
	Ext.getCmp('detail-differ-distax').setValue(Share.RetainDecimal(oriDT-parseFloat(DT1)-parseFloat(DT2)));
	}       		
};

function ResetPanelParam(model,newVal){//重置明细拆分相关参数
		if(newVal == "1"){//拆分单价
		Ext.getCmp('detail2-price').setReadOnly(false);
		Ext.getCmp('detail2-quantity').setReadOnly(true);
		Ext.getCmp('detail2-NTA').setReadOnly(true);
		Ext.getCmp('detail2-TA').setReadOnly(true);
		Ext.getCmp('detail2-price').on('change',TypeOneCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeTwoCount);
 		Ext.getCmp('detail2-TA').un('change',TypeThreeCount);
 		Ext.getCmp('detail2-quantity').un('change',TypeFourCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeFiveCount);
 		Ext.getCmp('detail2-TA').un('change',TypeSixCount);
 		
		Ext.getCmp('detail2-price').setValue(0);
		Ext.getCmp('detail2-quantity').setValue(model.get('QUANTITY'));
		Ext.getCmp('detail2-NTA').setValue(0);
		Ext.getCmp('detail2-TA').setValue(0);
		
		Ext.getCmp('detail2-amount').setValue(0);
		Ext.getCmp('detail2-tax').setValue(0);
		Ext.getCmp('detail2-disamount').setValue(0);
		Ext.getCmp('detail2-distax').setValue(0);
			
 		Ext.getCmp('detail2-price').focus(true,true); 		
		}else if(newVal == "2"){//输入不含税额（单价不变）
		Ext.getCmp('detail2-price').setReadOnly(true);
		Ext.getCmp('detail2-quantity').setReadOnly(true);
		Ext.getCmp('detail2-NTA').setReadOnly(false);
		Ext.getCmp('detail2-TA').setReadOnly(true);
		Ext.getCmp('detail2-price').un('change',TypeOneCount);
 		Ext.getCmp('detail2-NTA').on('change',TypeTwoCount);
 		Ext.getCmp('detail2-TA').un('change',TypeThreeCount);
 		Ext.getCmp('detail2-quantity').un('change',TypeFourCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeFiveCount);
 		Ext.getCmp('detail2-TA').un('change',TypeSixCount);
 		
		Ext.getCmp('detail2-price').setValue(model.get('PRICE_CUR'));
		Ext.getCmp('detail2-quantity').setValue(0);
		Ext.getCmp('detail2-NTA').setValue(0);
		Ext.getCmp('detail2-TA').setValue(0);
		
		Ext.getCmp('detail2-amount').setValue(0);
		Ext.getCmp('detail2-tax').setValue(0);
		Ext.getCmp('detail2-disamount').setValue(0);
		Ext.getCmp('detail2-distax').setValue(0);
		
 		Ext.getCmp('detail2-NTA').focus(true,true); 
	}else if(newVal == "3"){//输入含税额（单价不变）
		Ext.getCmp('detail2-price').setReadOnly(true);
		Ext.getCmp('detail2-quantity').setReadOnly(true);
		Ext.getCmp('detail2-NTA').setReadOnly(true);
		Ext.getCmp('detail2-TA').setReadOnly(false);
		Ext.getCmp('detail2-price').un('change',TypeOneCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeTwoCount);
 		Ext.getCmp('detail2-TA').on('change',TypeThreeCount);
 		Ext.getCmp('detail2-quantity').un('change',TypeFourCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeFiveCount);
 		Ext.getCmp('detail2-TA').un('change',TypeSixCount);
 		
		Ext.getCmp('detail2-price').setValue(model.get('PRICE_CUR'));
		Ext.getCmp('detail2-quantity').setValue(0);
		Ext.getCmp('detail2-NTA').setValue(0);
		Ext.getCmp('detail2-TA').setValue(0);
		
		Ext.getCmp('detail2-amount').setValue(0);
		Ext.getCmp('detail2-tax').setValue(0);
		Ext.getCmp('detail2-disamount').setValue(0);
		Ext.getCmp('detail2-distax').setValue(0);
 		
 		Ext.getCmp('detail2-TA').focus(true,true); 
	}else if(newVal == "4"){//拆分数量
		Ext.getCmp('detail2-price').setReadOnly(true);
		Ext.getCmp('detail2-quantity').setReadOnly(false);
		Ext.getCmp('detail2-NTA').setReadOnly(true);
		Ext.getCmp('detail2-TA').setReadOnly(true);
		Ext.getCmp('detail2-price').un('change',TypeOneCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeTwoCount);
 		Ext.getCmp('detail2-TA').un('change',TypeThreeCount);
 		Ext.getCmp('detail2-quantity').on('change',TypeFourCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeFiveCount);
 		Ext.getCmp('detail2-TA').un('change',TypeSixCount);
 		
		Ext.getCmp('detail2-price').setValue(model.get('PRICE_CUR'));
		Ext.getCmp('detail2-quantity').setValue(0);
		Ext.getCmp('detail2-NTA').setValue(0);
		Ext.getCmp('detail2-TA').setValue(0);
		
		Ext.getCmp('detail2-amount').setValue(0);
		Ext.getCmp('detail2-tax').setValue(0);
		Ext.getCmp('detail2-disamount').setValue(0);
		Ext.getCmp('detail2-distax').setValue(0);
 		
 		Ext.getCmp('detail2-quantity').focus(true,true); 
	}else if(newVal == "5"){//输入不含税额（数量不变）
		Ext.getCmp('detail2-price').setReadOnly(true);
		Ext.getCmp('detail2-quantity').setReadOnly(true);
		Ext.getCmp('detail2-NTA').setReadOnly(false);
		Ext.getCmp('detail2-TA').setReadOnly(true);
		Ext.getCmp('detail2-price').un('change',TypeOneCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeTwoCount);
 		Ext.getCmp('detail2-TA').un('change',TypeThreeCount);
 		Ext.getCmp('detail2-quantity').un('change',TypeFourCount);
 		Ext.getCmp('detail2-NTA').on('change',TypeFiveCount);
 		Ext.getCmp('detail2-TA').un('change',TypeSixCount);
 		
		Ext.getCmp('detail2-price').setValue(0);
		Ext.getCmp('detail2-quantity').setValue(model.get('QUANTITY'));
		Ext.getCmp('detail2-NTA').setValue(0);
		Ext.getCmp('detail2-TA').setValue(0);
		
		Ext.getCmp('detail2-amount').setValue(0);
		Ext.getCmp('detail2-tax').setValue(0);
		Ext.getCmp('detail2-disamount').setValue(0);
		Ext.getCmp('detail2-distax').setValue(0);				
 		
 		Ext.getCmp('detail2-NTA').focus(true,true);
	}else if(newVal == "6"){//输入含税额（数量不变）
		Ext.getCmp('detail2-price').setReadOnly(true);
		Ext.getCmp('detail2-quantity').setReadOnly(true);
		Ext.getCmp('detail2-NTA').setReadOnly(true);
		Ext.getCmp('detail2-TA').setReadOnly(false);
		Ext.getCmp('detail2-price').un('change',TypeOneCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeTwoCount);
 		Ext.getCmp('detail2-TA').un('change',TypeThreeCount);
 		Ext.getCmp('detail2-quantity').un('change',TypeFourCount);
 		Ext.getCmp('detail2-NTA').un('change',TypeFiveCount);
 		Ext.getCmp('detail2-TA').on('change',TypeSixCount);
 		
		Ext.getCmp('detail2-price').setValue(0);
		Ext.getCmp('detail2-quantity').setValue(model.get('QUANTITY'));
		Ext.getCmp('detail2-NTA').setValue(0);
		Ext.getCmp('detail2-TA').setValue(0);
		
		Ext.getCmp('detail2-amount').setValue(0);
		Ext.getCmp('detail2-tax').setValue(0);
		Ext.getCmp('detail2-disamount').setValue(0);
		Ext.getCmp('detail2-distax').setValue(0);
		
 		Ext.getCmp('detail2-TA').focus(true,true);
	}
	//明细1            	
	Ext.getCmp('detail1-price').setValue(model.get('PRICE_CUR'));
	Ext.getCmp('detail1-quantity').setValue(model.get('QUANTITY'));	
	Ext.getCmp('detail1-NTA').setValue(Share.RetainDecimal(parseFloat(model.get('AMOUNT_CUR'))/ManualX));
	Ext.getCmp('detail1-TA').setValue(Share.RetainDecimal(parseFloat(model.get('AMOUNT_CUR'))*(1+parseFloat(model.get('TAX_RATE')))/ManualX));
	Ext.getCmp('detail1-amount').setValue(model.get('AMOUNT_CUR'));
	Ext.getCmp('detail1-tax').setValue(model.get('TAX_AMOUNT'));
	Ext.getCmp('detail1-disamount').setValue(model.get('DISCOUNT_SUM'));
	Ext.getCmp('detail1-distax').setValue(model.get('DISCOUNT_TAX'));
	//暂存值
	ManualDetailPrice = parseFloat(model.get('PRICE_CUR'));
	ManualDetailQuantity = parseFloat(model.get('QUANTITY'));
	ManualDetailNTA = Share.RetainDecimal(parseFloat(model.get('AMOUNT_CUR'))/ManualX);
	ManualDetailTA = Share.RetainDecimal(parseFloat(model.get('AMOUNT_CUR'))*(1+parseFloat(model.get('TAX_RATE')))/ManualX);
	//尾差
//	Ext.getCmp('detail-amount').setValue(model.get('AMOUNT_CUR'));
	Ext.getCmp('detail-amount').setValue(Share.RetainDecimal(parseFloat(model.get('AMOUNT_CUR'))/ManualX));
	Ext.getCmp('detail-tax').setValue(model.get('TAX_AMOUNT'));
	Ext.getCmp('detail-disamount').setValue(model.get('DISCOUNT_SUM'));
	Ext.getCmp('detail-distax').setValue(model.get('DISCOUNT_TAX'));
	//正负尾差
	Ext.getCmp('detail-differ-amount').setValue(0);
	Ext.getCmp('detail-differ-tax').setValue(0);
	Ext.getCmp('detail-differ-disamount').setValue(0);
	Ext.getCmp('detail-differ-distax').setValue(0);
};

function CompareToDoc(model,newModel){
	var totalA = 0;
	var totalT = 0;
	var totalDA = 0;
	var totalDT = 0;
	var leftStore = Ext.getCmp('split-manual-left').getStore();
	for(var i = 0 ;i<leftStore.getCount();i++){
		var A = parseFloat(leftStore.getAt(i).get('PRICE_CUR'))*parseFloat(leftStore.getAt(i).get('QUANTITY'));
        var DA = A*(parseFloat(leftStore.getAt(i).get('DISCOUNT_RATE')));
        totalA += ((A-DA)/ManualX);
        var T = A*parseFloat(leftStore.getAt(i).get('TAX_RATE'))/ManualX;
    	var DT = A*parseFloat(leftStore.getAt(i).get('DISCOUNT_RATE'))*parseFloat(leftStore.getAt(i).get('TAX_RATE'))/ManualX;
    	totalT += (T-DT);
		totalDA += parseFloat(leftStore.getAt(i).get('DISCOUNT_SUM'));
		totalDT += parseFloat(leftStore.getAt(i).get('DISCOUNT_TAX'));
	}
	var A = parseFloat(Ext.getCmp('manual-amount').getValue());
	var T = parseFloat(Ext.getCmp('manual-tax').getValue());
	var DA = parseFloat(Ext.getCmp('manual-disamount').getValue());
	var DT = parseFloat(Ext.getCmp('manual-distax').getValue());
	Ext.getCmp('manual-differ-amount').setValue(Share.RetainDecimal(A-Share.RetainDecimal(totalA)));
	Ext.getCmp('manual-differ-tax').setValue(Share.RetainDecimal(T-Share.RetainDecimal(totalT)));
	Ext.getCmp('manual-differ-disamount').setValue(Share.RetainDecimal(DA-Share.RetainDecimal(totalDA)));
	Ext.getCmp('manual-differ-distax').setValue(Share.RetainDecimal(DT-Share.RetainDecimal(totalDT)));
};
Ext.define('HIT.controller.DealManualSplit', {
    extend: 'Ext.app.Controller',
    models: [
        'ManualInvDetail'
    ],
    stores: [
        'splitDetailLeft'
    ],
    views: [
         'DealSplitManualWin'
        ,'DealSplitManualParamWin'
    ],

    onButtonClick: function(button, e, options) {
    	var tabpanelId = 'tab-deal-search';
        var grid = Ext.getCmp(tabpanelId);
        if (grid == undefined) {
            return;
        }
       if (button.id == 'split-manual'){//手工拆分,
        	var selModel = grid.getSelectionModel();
            if (selModel.hasSelection()) {
            	if(selModel.getCount() < 1)
            	{   
            		Share.ShowMessage("I0011","需要操作的行");
            		return;
            	}
            	else if(selModel.getCount() > 1){
            		Share.ShowMessage("I0009","手工拆分");
            		return;
            	}
                var model = selModel.getSelection()[0];
            	
            	var doc_locked_code = model.get('doc_locked_code');
            	if(doc_locked_code != 0 ){
            		Share.ShowMessage("E0027");
            		return;
            	}          	
                
                if(model.get('doc_status_code') != 0){//判断是否“未开票”票据
                	Share.ShowMessage("I0011","未开票票据");
                	return;
                }
            	var c_win = Ext.create('widget.splitmanualwin');
             	Share.showWindow(c_win,true);
             	MSplitGroup = 0;//标记手工拆分当前最大组数清零
            	var doc_no = model.get('document_no');
            	Ext.getCmp('manual-docNo').setValue(doc_no);
            	Ext.getCmp('manual-custName').setValue(model.get('cust_name_chn'));
            	Ext.getCmp('manual-amount').setValue(model.get('total_amount'));//不含税金额
            	Ext.getCmp('manual-tax').setValue(model.get('tax_amount')); //税额       
                ManualInvType = model.get('doc_type_code') == "" ? "N_VAT" : model.get('doc_type_code');            	
            	var leftStore = Ext.getCmp('split-manual-left').getStore();
            	leftStore.on('beforeload', function(store) {
         			Ext.apply(store.proxy.extraParams , {doc_no : doc_no});        			
        		});
            	leftStore.load({
               		callback:function(records, options, success){          			
          			    var DA = 0,DT = 0;
            	        for(var i=0;i<records.length;i++){
    				        DA += records[i].data["DISCOUNT_SUM"];
    				        DT += records[i].data["DISCOUNT_TAX"];
            	        }
            	        Ext.getCmp('manual-disamount').setValue(Share.RetainDecimal(DA));//折扣金额
            	        Ext.getCmp('manual-distax').setValue(Share.RetainDecimal(DT));//折扣税额
                	  }   
            	   }); 
            	leftStore.reload({
               		callback:function(records, options, success){          			
          			    var DA = 0,DT = 0;
            	        for(var i=0;i<records.length;i++){
    				        DA += records[i].data["DISCOUNT_SUM"];
    				        DT += records[i].data["DISCOUNT_TAX"];
            	        }
            	        Ext.getCmp('manual-disamount').setValue(Share.RetainDecimal(DA));//折扣金额
            	        Ext.getCmp('manual-distax').setValue(Share.RetainDecimal(DT));//折扣税额
                	  }   
            	   });            	            	
            	
            }else{
            	Share.ShowMessage("I0011","需要操作的行");
				return;
            }    
        }else if(button.id == "split-menual-group"){//"分组"按钮
        	var leftGrid = Ext.getCmp('split-manual-left');
        	var leftSelModel = leftGrid.getSelectionModel();
            if (leftSelModel.hasSelection()){
            	var models = leftSelModel.getSelection();
            	MSplitGroup++;
            	for(var i=0;i<models.length;i++){
            		models[i].set('groupNum',MSplitGroup);
            	}
            	leftGrid.selModel.deselectAll();
            }else{
            	Share.ShowMessage("I0011","需要操作的行");
				return;
            }
        }else if(button.id == "split-menual-split"){//"拆分"按钮
        	var leftGrid = Ext.getCmp('split-manual-left');
        	var leftSelModel = leftGrid.getSelectionModel();
            if (leftSelModel.hasSelection()) {
            	if(leftSelModel.getCount() != 1)
            	{
            		Share.ShowMessage("I0011","一条记录操作");
            		return;
            	}
            	var model = leftSelModel.getSelection()[0];
            	var p = Ext.create('widget.splitmanualparampanel');
            	if(model.get('PRICE_WAY') == 1){
            		ManualX = 1 + parseFloat(model.get('TAX_RATE'));
            	}else{
            		ManualX=1;
            	}
            	ManualR = parseFloat(model.get('TAX_RATE'));
            	ManualDR = parseFloat(model.get('DISCOUNT_RATE'));
            	ResetPanelParam(model,"1");
//            	if(model.get('TAX_RATE') == 0){
//            		Ext.getCmp('split-type-tax-price').setDisabled(true);
//            		Ext.getCmp('split-type-tax-quantity').setDisabled(true);
//            	}else{
//            		Ext.getCmp('split-type-tax-price').setDisabled(false);
//            		Ext.getCmp('split-type-tax-quantity').setDisabled(false);
//            	}
            	Share.showWindow(p,true);
            	Ext.getCmp('detail2-price').focus(true,true);
            }else{
            	Share.ShowMessage("I0011","需要操作的行");
				return;
            }        	  
        }else if(button.id == "split-menual-sure"){//手动拆分"保存"
        	var leftGrid = Ext.getCmp('split-manual-left');
            var leftStore = leftGrid.getStore();
            var groupArr = {};
            var keyArr = [];
        	for(var i = 0 ; i < leftStore.getCount() ; i++){
        		var model = leftStore.getAt(i);
        		if(groupArr[model.get('groupNum')] == undefined){
        			groupArr[model.get('groupNum')] = [];
        			keyArr.push(model.get('groupNum'));
        		}
        		var modelList = {};
        		modelList['TAX_ITEM'] = model.get('TAX_ITEM');
        		modelList['GOODS_NAME'] = model.get('GOODS_NAME');
        		modelList['GOODS_CODE'] = model.get('GOODS_CODE');
        		modelList['GOODS_LINE'] = model.get('GOODS_LINE');
        		modelList['SPEC'] = model.get('SPEC');
        		modelList['GOODS_UNIT'] = model.get('GOODS_UNIT');
        		modelList['QUANTITY'] = model.get('QUANTITY');
        		modelList['PRICE_CUR'] = model.get('PRICE_CUR');
        		modelList['AMOUNT_CUR'] = model.get('AMOUNT_CUR');
        		modelList['TAX_RATE'] = model.get('TAX_RATE');
        		modelList['TAX_AMOUNT'] = model.get('TAX_AMOUNT');
        		modelList['DISCOUNT_RATE'] = model.get('DISCOUNT_RATE');
        		modelList['DISCOUNT_SUM'] = model.get('DISCOUNT_SUM');
        		modelList['DISCOUNT_TAX'] = model.get('DISCOUNT_TAX');       		        		
        		modelList['PRICE_WAY'] = model.get('PRICE_WAY');
        		groupArr[model.get('groupNum')].push(modelList);
        	}
        	var headId = grid.getSelectionModel().getSelection()[0].get("id_head_id");
        	var docNo = grid.getSelectionModel().getSelection()[0].get("document_no");
        	
            //判断新生成的票据是否超额，超行
        	var invConfig = Share.invoiceInitConfig(ManualInvType);
        	var MaxAmount = invConfig.MAX_AMOUNT;
        	var MaxLines = invConfig.MAX_LINE;
        	var maxLinesFlag = false;//标记是否有超行的票据
        	var maxAmountFlag = false;//标记是否有超额的票据
        	for(var j=0 ; j<keyArr.length;j++){//判断超行
        		if(groupArr[keyArr[j]].length > MaxLines){
        			maxLinesFlag = true;
        			break;
        		}
        	}
        	for(var j=0 ; j<keyArr.length;j++){//判断超额
        		var tAmount = 0;
        		for(var k=0; k<groupArr[keyArr[j]].length;k++){//一条票据求总金额
        			var temp = groupArr[keyArr[j]][k];
        			var X = 1;
        			if(temp["PRICE_WAY"] == 1){
						X = 1 + parseFloat(temp["TAX_RATE"]);
	            	}else{
	            		X=1;
					}
					var A = parseFloat(temp["PRICE_CUR"])*parseFloat(temp["QUANTITY"]);
					var DA = A*parseFloat(temp["DISCOUNT_RATE"]);
					tAmount += ((A-DA)/X);
        		}
        		if(Share.RetainDecimal(tAmount) > MaxAmount){
        			maxAmountFlag = true;
        			break;
        		}
        	}
        	var warnText = '';
        	if(maxAmountFlag){
        		warnText = Share.T('保存后有超最大金额的票据，继续保存操作吗？');
        	}else if(maxLinesFlag){
        		warnText = Share.T('保存后有超最大明细行的票据，继续保存操作吗？');
        	}
        	if(warnText != ''){
	        	Ext.MessageBox.confirm(Share.T('提示'),warnText,function(btn){
		            if(btn == 'yes'){  
	                    Share.AjaxRequest({
	           			url : ctx + '/splitManual', 
	           			params : {headId:headId
	           				     ,docNo:docNo
	           				     ,groupArr:Ext.encode(groupArr)
	           						},
	           			callback : function(json) {
	           				if (json.resultcode == '0') {
	           					grid.getStore().reload();
	           					button.up('window').close();
	           					Share.ShowMessage("I0006","");
	           				}else if(json.resultcode == '1'){
	           					Share.ShowMessage("E0007","拆分","20","拆分");
	           				}else {
	           					Share.ShowMessage("I0007","");
	           				}
	           			},
	           			falseFun : function(json) {Share.ShowMessage("I0007","");}
	           		}); 
		           }
		       }); 
        	}else{
              Share.AjaxRequest({
    			url : ctx + '/splitManual', 
    			params : {headId:headId
    				     ,docNo:docNo
    				     ,groupArr:Ext.encode(groupArr)
    						},
    			callback : function(json) {
    				if (json.resultcode == '0') {
    					grid.getStore().reload();
    					button.up('window').close();
    					Share.ShowMessage("I0006","");
    				}else if(json.resultcode == '1'){
       					Share.ShowMessage("E0007","拆分","20","拆分");
       				}else {
    					Share.ShowMessage("I0007","");
    					}
    			},
    			falseFun : function(json) {Share.ShowMessage("I0007","");}
    		}); 
        	}    
        }else if(button.id == "split-detail-continue"){//拆分明细-》“继续拆分”
        	var  form = button.up('window').down('form');
//        	console.log(form);
        	var basicForm = form.getForm();
            if (!basicForm.isValid()) {
                return;
            }
        	if(Ext.getCmp('detail2-quantity').getValue() == 0 || Ext.getCmp('detail2-price').getValue() == 0
        	  ||Ext.getCmp('detail1-quantity').getValue() == 0 || Ext.getCmp('detail1-price').getValue() == 0){
        		Share.ShowMessage("I0025");
        		return;
        	}
        	var radio = Ext.getCmp('split-type-radio').getValue();
        	var newVal = radio['split-type'];
        	var leftGrid = Ext.getCmp('split-manual-left');
            var leftSelModel = leftGrid.getSelectionModel();
            var model = leftSelModel.getSelection()[0];
            var newModel = model.copy();
            model.set('PRICE_CUR',Ext.getCmp('detail1-price').getValue());
            model.set('QUANTITY',Ext.getCmp('detail1-quantity').getValue());
            model.set('AMOUNT_CUR',Ext.getCmp('detail1-amount').getValue());
            model.set('TAX_AMOUNT',Ext.getCmp('detail1-tax').getValue());
            model.set('DISCOUNT_SUM',Ext.getCmp('detail1-disamount').getValue());
            model.set('DISCOUNT_TAX',Ext.getCmp('detail1-distax').getValue());
           
            newModel.set('PRICE_CUR',Ext.getCmp('detail2-price').getValue());
            newModel.set('QUANTITY',Ext.getCmp('detail2-quantity').getValue());
            newModel.set('AMOUNT_CUR',Ext.getCmp('detail2-amount').getValue());
            newModel.set('TAX_AMOUNT',Ext.getCmp('detail2-tax').getValue());
            newModel.set('DISCOUNT_SUM',Ext.getCmp('detail2-disamount').getValue());
            newModel.set('DISCOUNT_TAX',Ext.getCmp('detail2-distax').getValue());
            var leftStore = leftGrid.getStore(); 
            var count = leftStore.getCount();
            leftStore.insert(count, newModel);
            CompareToDoc(model,newModel);
            ResetPanelParam(model,newVal);           
        }else if(button.id == 'split-detail-sure'){//拆分明细“确定”按钮
        	var  form = button.up('window').down('form');
//        	console.log(form);
        	var basicForm = form.getForm();
            if (!basicForm.isValid()) {
                return;
            }
        	if(Ext.getCmp('detail2-quantity').getValue() == 0 || Ext.getCmp('detail2-price').getValue() == 0
              	  ||Ext.getCmp('detail1-quantity').getValue() == 0 || Ext.getCmp('detail1-price').getValue() == 0){
        		    Share.ShowMessage("I0025");
              		return;
              	}
              	var leftGrid = Ext.getCmp('split-manual-left');
                  var leftSelModel = leftGrid.getSelectionModel();
                  var model = leftSelModel.getSelection()[0];
                  var newModel = model.copy();
                  model.set('PRICE_CUR',Ext.getCmp('detail1-price').getValue());
                  model.set('QUANTITY',Ext.getCmp('detail1-quantity').getValue());
                  model.set('AMOUNT_CUR',Ext.getCmp('detail1-amount').getValue());
                  model.set('TAX_AMOUNT',Ext.getCmp('detail1-tax').getValue());
                  model.set('DISCOUNT_SUM',Ext.getCmp('detail1-disamount').getValue());
                  model.set('DISCOUNT_TAX',Ext.getCmp('detail1-distax').getValue());
                 
                  newModel.set('PRICE_CUR',Ext.getCmp('detail2-price').getValue());
                  newModel.set('QUANTITY',Ext.getCmp('detail2-quantity').getValue());
                  newModel.set('AMOUNT_CUR',Ext.getCmp('detail2-amount').getValue());
                  newModel.set('TAX_AMOUNT',Ext.getCmp('detail2-tax').getValue());
                  newModel.set('DISCOUNT_SUM',Ext.getCmp('detail2-disamount').getValue());
                  newModel.set('DISCOUNT_TAX',Ext.getCmp('detail2-distax').getValue());
                  var leftStore = leftGrid.getStore(); 
                  var count = leftStore.getCount();
                  leftStore.insert(count, newModel);
                  CompareToDoc(model,newModel);
                  button.up('window').close();
        }
    },

    save : function(win) {
    	 var grid=Ext.getCmp('tab-deal-search');       
         if (grid == undefined) {
  			return;
  		}
         
	},
    

    init: function(application) {
        this.control({
            "button": {
                click: this.onButtonClick
            }           
        });
    }

});
